<template>
  <div class="app-container">
    <!-- 顶部搜索 -->
    <el-form :model="queryParams" ref="queryForm" size="small" v-show="showSearch" label-width="100px">
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="知识产权名称:" prop="zscqName">
            <el-input
              v-model="queryParams.zscqName"
              placeholder="请输入知识产权名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="类别:" prop="lb">
            <el-input
              v-model="queryParams.lb"
              placeholder="请输入类别"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="授权/专利号:" prop="sqZlh">
            <el-input
              v-model="queryParams.sqZlh"
              placeholder="请输入授权/专利号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="知识产权编号:" prop="zscqBh">
            <el-input
              v-model="queryParams.zscqBh"
              placeholder="请输入知识产权编号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="获得方式:" prop="hdFs">
            <el-input
              v-model="queryParams.hdFs"
              placeholder="请输入获得方式"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <!-- 顶部按钮 -->
    <el-form :model="queryParams" size="mini" label-width="170px">
      <el-row :gutter="10" class="mb8" style="display: flex; justify-content: flex-end;margin-bottom: -8px">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['gqgl:ipb:add']"
            :disabled="!YwJhByGqgl || (YwJhByGqgl !== true)"
          >新增
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['gqgl:ipb:edit']"
          >修改
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['gqgl:ipb:remove']"
          >删除
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['gqgl:ipb:export']"
          >导出
          </el-button>
        </el-col>
        <el-col :span="1.5" style="margin-left: auto;">
          <el-form-item label="认定计划:" prop="xmZt">
            <vxe-select @change="getList" size="mini" v-model="queryParams.rdNd" placeholder="请选择认定计划">
              <vxe-option v-for="item in ywJhList" :key="item.nd" :value="item.nd"
                          :label="item.jhLb"></vxe-option>
            </vxe-select>
          </el-form-item>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
    </el-form>

    <!-- 表格 -->
    <el-table @row-click="clickRow" ref="table" highlight-current-row
              :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}"
              height="300" class="fixedTableTwo" v-loading="loading" :data="ipbList"
              @selection-change="handleSelectionChange">
      <!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column label="序号" align="center" type="index" :index="indexMethod"/>
      <el-table-column label="认定年度" align="center" prop="rdNd"/>
      <el-table-column label="授权年度" align="center" prop="sqNd"/>
      <el-table-column label="知识产权名称" align="center" prop="zscqName">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleButtonClick(scope.row)"
          >
            <el-link type="primary">{{ scope.row.zscqName }}</el-link>
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="知识产权编号" align="center" prop="zscqBh"/>
      <el-table-column label="授权/专利号" align="center" prop="sqZlh"/>
      <el-table-column label="类别" align="center" prop="lb"/>
      <el-table-column label="授权日期" align="center" prop="sqRq" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sqRq, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="获得方式" align="center" prop="hdFs"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['gqgl:ipb:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['gqgl:ipb:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改IP对话框 -->
    <vxe-modal :title="title" v-model="open" width="60%" showFooter show-zoom resize>
      <el-form ref="formDialog" :model="form" :rules="rules" label-width="160px">
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="知识产权名称" prop="zscqName">
              <el-dropdown trigger="click" style="width: 100%;">
                <el-input :readonly="true" v-model="form.zscqName" placeholder="请输入知识产权名称">
                  <el-button slot="append" icon="el-icon-search"></el-button>
                </el-input>
                <el-dropdown-menu slot="dropdown" style="width: 42vh;font-size: 19px">
                  <el-dropdown-item @click.native="handleZl('实用专利')">实用专利</el-dropdown-item>
                  <el-dropdown-item @click.native="handleZl('发明专利')">发明专利</el-dropdown-item>
                  <el-dropdown-item @click.native="handleZl('外观专利')">外观专利</el-dropdown-item>
                  <el-dropdown-item @click.native="handleZl('软件著作权')">软件著作权</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类别" prop="lb">
              <el-input :readonly="true" v-model="form.lb" placeholder="请输入类别"/>
            </el-form-item>
            <el-form-item label="知识产权ID" prop="zscqId">
              <el-input :readonly="true" v-model="form.zscqId" placeholder="请输入知识产权ID"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="认定年度" prop="rdNd">
              <el-input :readonly="true" v-model="form.rdNd" placeholder="请输入认定年度"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="知识产权编号" prop="zscqBh">
              <el-input disabled v-model="form.zscqBh" placeholder="待生成..."/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="授权/专利号" prop="sqZlh">
              <el-input :readonly="true" v-model="form.sqZlh" placeholder="请输入授权/专利号"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="授权日期" prop="sqRq">
              <el-date-picker :readonly="true" clearable
                              v-model="form.sqRq"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择授权日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="授权年度" prop="sqNd">
              <el-input :readonly="true" v-model="form.sqNd" placeholder="请输入授权年度"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="获得方式" prop="hdFs">
              <el-input v-model="form.hdFs" placeholder="请输入获得方式"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="先进性及支撑作用说明" prop="xjxJZczy">
          <el-input :rows="5" v-model="form.xjxJZczy" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </vxe-modal>

    <!-- 根据专利类型选择已授权的专利 -->
    <ZlXzByZlLx ref="zlXzByZlLx"/>

    <!-- 选择已登记的软著 -->
    <SqRzXz ref="sqRzXz"/>

    <!-- IP表详情 -->
    <IpbXq ref="ipbXq"/>
  </div>
</template>

<script>
import {addIpb, delIpb, getIpb, listIpb, updateIpb} from "@/api/gqgl/ipb";
import {selectYwJh} from "@/api/xtgl/ywjh";
import ZlXzByZlLx from "@/views/components/ZlXz/ZlXzByZlLx.vue";
import SqRzXz from "@/views/components/RzXz/SqRzXz.vue";
import IpbXq from "@/views/gqgl/ipb/components/IpbXq.vue";

export default {
  name: "Ipb",
  components: {IpbXq, SqRzXz, ZlXzByZlLx},
  provide() {
    return {
      updateZlLabel: (val) => {
        this.form.zscqName = val.zlName
        this.form.lb = val.zlLx
        this.form.sqZlh = val.sqZlh
        this.form.sqRq = val.sqTime
        let date = new Date(val.sqTime); // 将字符串转换为Date对象
        this.form.sqNd = date.getFullYear(); // 从Date对象中获取年份
        this.form.zscqId = val.zlsbId

        this.$refs['formDialog'].validateField('zscqName'); // formDialog 是表单的 ref，rzsqId 是要验证的字段名
        this.$refs['formDialog'].validateField('lb'); // formDialog 是表单的 ref，rzsqId 是要验证的字段名
      },
      updateSqRzLabel: (val) => {
        this.form.zscqName = val.rjName
        this.form.lb = '软件著作权'
        this.form.sqZlh = val.djh
        this.form.sqRq = val.djRq
        let date = new Date(val.djRq); // 将字符串转换为Date对象
        this.form.sqNd = date.getFullYear(); // 从Date对象中获取年份
        this.form.zscqId = val.rjsqdjId

        this.$refs['formDialog'].validateField('zscqName'); // formDialog 是表单的 ref，rzsqId 是要验证的字段名
        this.$refs['formDialog'].validateField('lb'); // formDialog 是表单的 ref，rzsqId 是要验证的字段名
      }
    };
  },
  data() {
    return {
      YwJhByGqgl: false,
      ywJhList: null, //业务计划列表
      selectedRow: null, // 保存当前选中的行
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // IP表格数据
      ipbList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        zscqName: null,
        lb: null,
        rdNd: (new Date().getFullYear()).toString(),
        zscqBh: null,
        sqZlh: null,
        hdFs: null,
        idWhere: null,
        userId: null,
        deptId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        zscqName: [{required: true, message: "请点击后选择知识产权类别", trigger: "blur"}],
        lb: [{required: true, message: "类别不能为空", trigger: "blur"}],
        rdNd: [{required: true, message: "认定年度不能为空", trigger: "blur"}],
      },
    };
  },
  created() {
    this.getList();
    this.selectYwJhList()
  },
  methods: {
    //打开IP表详情
    handleButtonClick(row) {
      this.$refs.ipbXq.handleOpen(row);
    },
    //选择专利或者软著
    handleZl(zlLx) {
      if (zlLx == '软件著作权') {
        this.$refs.sqRzXz.handleOpen()
      } else {
        this.$refs.zlXzByZlLx.handleOpen(zlLx);
      }
    },
    //不分页查询业务计划中的加计扣除计划列表
    selectYwJhList() {
      let query = {
        pageNum: 1,
        pageSize: 50,
        ywSb: '平台资质',
        ywLx: '高企认定',
        zt: '启用'
      }
      selectYwJh(query).then(res => {
        this.ywJhList = res.rows;
      });
    },
    //点击行 获取行
    clickRow(row) {
      this.selectedRow = row; // 更新选中的行
      const table = this.$refs.table;
      const selectedRows = table.selection;

      // 否则取消之前选中的行，并选中当前点击的行
      selectedRows.forEach(selectedRow => {
        table.toggleRowSelection(selectedRow);
      });
      table.toggleRowSelection(row);
    },
    /** 自定义序号 */
    indexMethod(index) {
      let pageNum = this.queryParams.pageNum - 1;
      if ((pageNum !== -1 && pageNum !== 0)) {
        return (index + 1) + (pageNum * this.queryParams.pageSize);
      } else {
        return (index + 1)
      }
    },
    /** 查询IP列表 */
    getList() {
      this.loading = true;
      listIpb(this.queryParams).then(response => {
        let query = {
          pageNum: 1,
          pageSize: 50,
          nd: this.queryParams.rdNd,
          ywSb: '平台资质',
          ywLx: '高企认定',
        }
        selectYwJh(query).then(res => {
          let ywjhList = res.rows;
          if (ywjhList[0].jhZt == '进行中') {
            this.YwJhByGqgl = true
          } else {
            this.YwJhByGqgl = false
          }

          this.ipbList = response.rows;
          this.total = response.total;
          this.loading = false;
        });

      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ipbId: null,
        zscqName: null,
        lb: null,
        rdNd: null,
        zscqBh: null,
        sqZlh: null,
        sqRq: null,
        sqNd: null,
        hdFs: null,
        xjxJZczy: null,
        idWhere: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        userId: null,
        deptId: null,
        zscqId: null,
      };
      this.resetForm("formDialog");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.ipbId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.rdNd = this.queryParams.rdNd
      this.open = true;
      this.title = "添加IP";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const ipbId = row.ipbId || this.ids
      getIpb(ipbId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改IP";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["formDialog"].validate(valid => {
        if (valid) {
          if (this.form.idWhere != null) {
            updateIpb(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            console.log('asdasdasdasd')
            addIpb(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ipbIds = row.ipbId || this.ids;
      this.$modal.confirm('确定删除选中记录？').then(function () {
        return delIpb(ipbIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('gqgl/ipb/export', {
        ...this.queryParams
      }, `ipb_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>
